package com.example.coffeeseller_2.views.customviews;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.coffeeseller_2.R;

import cof.ac.inter.CoffMsger;
import cof.ac.inter.DebugAction;
import cof.ac.inter.Result;

/**
 * Created by Administrator on 2018/6/22.
 */

public class DebugDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private Button mProductTest;
    private Button mMachine_cleaning;
    private Button mWaterBox_cleaning;
    private Button mBoiler_cleaning;
    private Button mBunker1_drop;
    private Button mBunker2_drop;
    private Button mBunker3_drop;
    private Button mBunker4_drop;
    private Button mBunker5_drop;
    private Button mBunker6_drop;
    private Button mAgitator1_cleaning;
    private Button mAgitator2_cleaning;
    private Button mAgitator3_cleaning;
    private Button mAgitator4_cleaning;
    private Button mGrinding_beans;
    private Button mOut_hotwater;
    private Button mDivide_cup;
    private Button mRemove_out;
    private Button mRemove_in;
    private Button mRemove_cup_system;
    private Button mBurst_bubble_up;
    private Button mBurst_bubble_remove;
    private Button mBurst_bubble_test;
    private Button mBurst_bubble_cleaning;
    private Button mOpen_bigdoor;
    private Button mClose_bigdoor;
    private Button mOpen_smalldoor;
    private Button mClose_smalldoor;
    private Button mDismiss;
    private TextView debugMsg;
    private int DebugCD = 2000;
    private long lastDebugTime;

    public DebugDialog(@NonNull Context context) {
        super(context);
    }

    public DebugDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        mContext = context;
        init();
    }

    //  UI界面优化
    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.debug_dialog_layout, null, false);
        setContentView(view);
        bindViews(view);
        initListener();
    }


    private void initListener() {
        setCanceledOnTouchOutside(false);
        mProductTest.setOnClickListener(this);
        mMachine_cleaning.setOnClickListener(this);
        mWaterBox_cleaning.setOnClickListener(this);
        mBoiler_cleaning.setOnClickListener(this);
        mBunker1_drop.setOnClickListener(this);
        mBunker2_drop.setOnClickListener(this);
        mBunker3_drop.setOnClickListener(this);
        mBunker4_drop.setOnClickListener(this);
        mBunker5_drop.setOnClickListener(this);
        mBunker6_drop.setOnClickListener(this);
        mAgitator1_cleaning.setOnClickListener(this);
        mAgitator2_cleaning.setOnClickListener(this);
        mAgitator3_cleaning.setOnClickListener(this);
        mAgitator4_cleaning.setOnClickListener(this);
        mGrinding_beans.setOnClickListener(this);
        mOut_hotwater.setOnClickListener(this);
        mDivide_cup.setOnClickListener(this);
        mRemove_out.setOnClickListener(this);
        mRemove_in.setOnClickListener(this);
        mRemove_cup_system.setOnClickListener(this);
        mBurst_bubble_up.setOnClickListener(this);
        mBurst_bubble_remove.setOnClickListener(this);
        mBurst_bubble_test.setOnClickListener(this);
        mBurst_bubble_cleaning.setOnClickListener(this);
        mOpen_bigdoor.setOnClickListener(this);
        mClose_bigdoor.setOnClickListener(this);
        mOpen_smalldoor.setOnClickListener(this);
        mClose_smalldoor.setOnClickListener(this);
        mDismiss.setOnClickListener(this);
    }

    private void bindViews(View view) {

        mProductTest = (Button) view.findViewById(R.id.productTest);
        mMachine_cleaning = (Button) view.findViewById(R.id.machine_cleaning);
        mWaterBox_cleaning = (Button) view.findViewById(R.id.waterBox_cleaning);
        mBoiler_cleaning = (Button) view.findViewById(R.id.boiler_cleaning);
        mBunker1_drop = (Button) view.findViewById(R.id.bunker1_drop);
        mBunker2_drop = (Button) view.findViewById(R.id.bunker2_drop);
        mBunker3_drop = (Button) view.findViewById(R.id.bunker3_drop);
        mBunker4_drop = (Button) view.findViewById(R.id.bunker4_drop);
        mBunker5_drop = (Button) view.findViewById(R.id.bunker5_drop);
        mBunker6_drop = (Button) view.findViewById(R.id.bunker6_drop);
        mAgitator1_cleaning = (Button) view.findViewById(R.id.agitator1_cleaning);
        mAgitator2_cleaning = (Button) view.findViewById(R.id.agitator2_cleaning);
        mAgitator3_cleaning = (Button) view.findViewById(R.id.agitator3_cleaning);
        mAgitator4_cleaning = (Button) view.findViewById(R.id.agitator4_cleaning);
        mGrinding_beans = (Button) view.findViewById(R.id.grinding_beans);
        mOut_hotwater = (Button) view.findViewById(R.id.out_hotwater);
        mDivide_cup = (Button) view.findViewById(R.id.divide_cup);
        mRemove_out = (Button) view.findViewById(R.id.remove_out);
        mRemove_in = (Button) view.findViewById(R.id.remove_in);
        mRemove_cup_system = (Button) view.findViewById(R.id.remove_cup_system);
        mBurst_bubble_up = (Button) view.findViewById(R.id.Burst_bubble_up);
        mBurst_bubble_remove = (Button) view.findViewById(R.id.Burst_bubble_remove);
        mBurst_bubble_test = (Button) view.findViewById(R.id.Burst_bubble_test);
        mBurst_bubble_cleaning = (Button) view.findViewById(R.id.Burst_bubble_cleaning);
        mOpen_bigdoor = (Button) view.findViewById(R.id.open_bigdoor);
        mClose_bigdoor = (Button) view.findViewById(R.id.close_bigdoor);
        mOpen_smalldoor = (Button) view.findViewById(R.id.open_smalldoor);
        mClose_smalldoor = (Button) view.findViewById(R.id.close_smalldoor);
        mDismiss = (Button) view.findViewById(R.id.dismiss);
        debugMsg = (TextView) view.findViewById(R.id.debugTitle);
    }

    @Override
    public void onClick(View v) {
        if (!iscanDebug()) {
            return;
        }
        switch (v.getId()) {
            case R.id.productTest:

           /*     new ProductTestDialog().showProductTestDialog(SingleMaterialList.getInstance(mContext).getCoffeeList()
                        , mContext, new ProductTestDialog.SendMkCoffeeCommandResult() {
                            @Override
                            public void getResult(Result result) {
                                if (result.getCode() == Result.SUCCESS) {
                                    Toast.makeText(mContext, "send mkCoffee comd success !", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(mContext, "send mkCoffee comd success !", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });*/
                break;
            case R.id.machine_cleaning:
                Action("清洗机器", DebugAction.WASH_MACHINE, 0, 0);
                break;
            case R.id.waterBox_cleaning:
                Action("清空水箱", DebugAction.CLEAR_WATERBOX, 0, 0);
                break;
            case R.id.boiler_cleaning:
                Action("清空锅炉", DebugAction.CLEAR_POT, 0, 0);
                break;
            case R.id.bunker1_drop:
                Action("料盒1掉粉2秒", DebugAction.OUT_INGREDIENT, 1, 20);
                break;
            case R.id.bunker2_drop:
                Action("料盒2掉粉2秒", DebugAction.OUT_INGREDIENT, 2, 20);
                break;
            case R.id.bunker3_drop:
                Action("料盒3掉粉2秒", DebugAction.OUT_INGREDIENT, 3, 20);
                break;
            case R.id.bunker4_drop:
                Action("料盒4掉粉2秒", DebugAction.OUT_INGREDIENT, 4, 20);
                break;
            case R.id.bunker5_drop:
                Action("料盒5掉粉2秒", DebugAction.OUT_INGREDIENT, 5, 20);
                break;
            case R.id.bunker6_drop:
                Action("料盒6掉粉2秒", DebugAction.OUT_INGREDIENT, 6, 20);
                break;
            case R.id.agitator1_cleaning:
                Action("清洗搅拌器1", DebugAction.CLEAR_MODULE, 1, 1);
                break;
            case R.id.agitator2_cleaning:
                Action("清洗搅拌器2", DebugAction.CLEAR_MODULE, 2, 1);
                break;
            case R.id.agitator3_cleaning:
                Action("清洗搅拌器3", DebugAction.CLEAR_MODULE, 3, 1);
                break;
            case R.id.agitator4_cleaning:
                //   Action(DebugAction.WASH_MACHINE,0,0);
                break;
            case R.id.grinding_beans:
                Action("磨豆测试", DebugAction.CRUSH_BEAN, 0, 20);
                break;
            case R.id.out_hotwater:
                Action("出热水", DebugAction.OUT_HOTWATER, 100, 0);
                break;
            case R.id.divide_cup:
                Action("分杯测试", DebugAction.DEVIDE_CUP, 1, 0);
                break;
            case R.id.remove_out:
                Action("移出杯托", DebugAction.MOVE_TRAY, 0, 1);
                break;
            case R.id.remove_in:
                Action("移入杯托", DebugAction.MOVE_TRAY, 0, 0);
                break;
            case R.id.remove_cup_system:
                Action("运杯系统测试", DebugAction.CUP_MOVE_SYSTEM, 1, 0);
                break;
            case R.id.Burst_bubble_up:
                Action("冲泡器提升", DebugAction.FLUSH_CTR, 0, 2);
                break;
            case R.id.Burst_bubble_remove:
                Action("冲泡器移出", DebugAction.FLUSH_CTR, 0, 0);
                break;
            case R.id.Burst_bubble_test:
                Action("冲泡器测试", DebugAction.FLUSH_CTR, 0, 1);
                break;
            case R.id.Burst_bubble_cleaning:
                Action("清洗冲泡器", DebugAction.CLEAR_MODULE, 0xAA, 1);
                break;
            case R.id.open_bigdoor:
                Action("打开大门", DebugAction.OPEN_DOOR, 0, 1);
                break;
            case R.id.close_bigdoor:
                Action("关闭大门", DebugAction.OPEN_DOOR, 0, 0);
                break;
            case R.id.open_smalldoor:
                Action("打开小门", DebugAction.CTR_LITTLEDOOR, 0, 1);
                break;
            case R.id.close_smalldoor:
                Action("关闭小门", DebugAction.CTR_LITTLEDOOR, 0, 0);
                break;
            case R.id.dismiss:
                dismiss();
                break;

        }
    }

    private boolean iscanDebug() {
        long crrentDebugTime = System.currentTimeMillis();
        if (crrentDebugTime - lastDebugTime < DebugCD) { //时间间隔小于2S时
            return false;
        } else {
            lastDebugTime = crrentDebugTime;
            return true;
        }

    }

    private void Action(String actionName, DebugAction action, int params1, int params2) {
        Result result = CoffMsger.getInstance().Debug(action, params1, params2);
        if (result != null) {
            if (result.getCode() == Result.SUCCESS) {
                debugMsg.setText(actionName + "执行成功请在" + DebugCD / 1000 + "秒后开始下一个测试");
                debugMsg.setTextColor(ContextCompat.getColor(mContext, R.color.sucessgreen));
            } else {
                debugMsg.setText(actionName + "执行失败请在" + DebugCD / 1000 + "秒后开始下一个测试");
                debugMsg.setTextColor(ContextCompat.getColor(mContext, R.color.red));
            }
        } else {
            debugMsg.setText("主控板通信失败,请检查串口是否被正确打开!");
            debugMsg.setTextColor(ContextCompat.getColor(mContext, R.color.red));
        }
    }
}
